package com.mobiletrain.org.motherkitch.shouye;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.mobiletrain.org.motherkitch.R;
import com.mobiletrain.org.motherkitch.utils.ImageLoad;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

public class TodayDetailActivity extends ActionBarActivity {
    private static final String TAG = "AAAAAA";
    private String path="http://www.qubaobei.com/ios/cf/dish_view.php?v=1&id=2127&user_id=4828718";
    private ImageView mImage;
    private TextView mTitle;
    private TextView mZuoPin;
    private TextView mCollect;
    private TextView mPost;
    private TextView mContent;
    private ListView mListView;
    private ArrayList<HashMap<String, String>> useList;
    private SimpleAdapter simpleAdapter;
    private ArrayList<HashMap<String, String>> cookList;
    private ListView mListView2;
    private SimpleAdapter simpleAdapter2;
    private ListView mListView3;
    private PeoplePostAdapter postAdapter;
    private ArrayList<Post> postList;
    private TextView mMore;
    private String id;
    private Button mBtnCol;
    private Button mBtnBuy;
    private Button mBtnSha;
    private Button mAddPost;
    private Button mUpload;
    private String imageFilePath;
    private File imageFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_today_detail);
        //getSupportActionBar().setTitle("妈妈厨房");
        Intent intent = getIntent();
        Bundle bun = intent.getBundleExtra("bun");
        String id = bun.getString("id");
        mImage = (ImageView) findViewById(R.id.detail_image);
        mTitle= (TextView) findViewById(R.id.detail_title);
        mContent= (TextView) findViewById(R.id.detail_content);
        mZuoPin= (TextView) findViewById(R.id.detail_zuopin);
        mCollect= (TextView) findViewById(R.id.detail_collect);
        mPost= (TextView) findViewById(R.id.detail_post);
        mListView= (ListView) findViewById(R.id.detail_listview);

        mListView2= (ListView) findViewById(R.id.detail_listview2);
        mListView3= (ListView) findViewById(R.id.detail_listview3);

        mAddPost= (Button) findViewById(R.id.post_addpost);
        mUpload= (Button) findViewById(R.id.post_upload);
        getData();
        mBtnCol= (Button) findViewById(R.id.detail_btn_collect);
        //收藏
        /*mBtnCol.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
                DbUtils db = DbUtils.create(TodayDetailActivity.this);

                try {
                    db.save(c);
                    Toast.makeText(TodayDetailActivity.this, "保存数据成功!", Toast.LENGTH_SHORT).show();
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        });*/
        //添加购物清单
        mBtnBuy= (Button) findViewById(R.id.detail_btn_buy);
        mBtnBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });


    }

    private void getData() {
        useList=new ArrayList<HashMap<String,String>>();
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET,path,new RequestCallBack<Object>() {
            @Override
            public void onSuccess(ResponseInfo<Object> info) {
                try {
                    JSONObject object = new JSONObject(info.result.toString());
                    JSONObject data = object.getJSONObject("data");
                    String title = data.getString("title");
                    mTitle.setText(title);
                    String content = data.getString("content");
                    mContent.setText(content);
                    String pic = data.getString("pic");
                    BitmapUtils bitmapUtils = ImageLoad.getBitmapUtils(TodayDetailActivity.this);
                    bitmapUtils.display(mImage,pic);
                    JSONArray use = data.getJSONArray("use");
                    for (int i = 0; i < use.length(); i++) {
                        Use u=new Use();
                        HashMap<String, String> map = new HashMap<String, String>();
                        JSONObject obj = (JSONObject) use.get(i);
                        String name = obj.getString("name");
                        String num = obj.getString("num");
                        u.setName(name);
                        u.setNum(num);
                        map.put("name",u.getName());
                        map.put("num", u.getNum());
                        useList.add(map);
                    }
                    //作品收藏
                    //dfkdfkddf
                    JSONObject num_info = data.getJSONObject("num_info");
                    String cook_num = num_info.getString("cook_num");
                    mZuoPin.setText(cook_num+"作品");
                    mPost.setText(num_info.getString("reply_num") + "评论");
                    mCollect.setText(num_info.getString("collect_num")+"收藏");



                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_today_detail, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
